package adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;

import net.ck.hzq.R;

/**
 * by BF00233 on 2018/12/10.
 */
public class DividerAdapter extends DelegateAdapter.Adapter<DividerAdapter.DividerVH> {

    private final Context mContext;
    private final int mHeight;
    private boolean mShow;

    public DividerAdapter(Context context, int height) {
        mContext = context;
        mHeight = height;
    }

    public void show(boolean show) {
        mShow = show;
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new SingleLayoutHelper();
    }

    @NonNull
    @Override
    public DividerVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new DividerVH(LayoutInflater.from(mContext).inflate(R.layout.item_divider, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull DividerVH holder, int i) {
        ViewGroup.LayoutParams layoutParams = holder.divider.getLayoutParams();
        layoutParams.height = mHeight;
        holder.divider.setLayoutParams(layoutParams);
    }

    @Override
    public int getItemCount() {
        return mShow ? 1 : 0;
    }

    class DividerVH extends RecyclerView.ViewHolder {
        private View divider;

        DividerVH(@NonNull View itemView) {
            super(itemView);
            divider = itemView.findViewById(R.id.divider);
        }
    }

}
